package com.deeper.customview.ui.c1.exercise;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.deeper.customview.BaseFragment;
import com.deeper.customview.R;

/**
 * 模拟验证码： 在 ImageView 绘制一个空心矩形， 随机产生 100 条干扰线，
 * 并随机生成 4 个数字显示在矩形框内。
 */
public class Chapter01Exercise02Fragment extends BaseFragment {
    private ImageView iv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_chapter01_exercise02, container, false);

        iv = root.findViewById(R.id.iv);
        Bitmap bmp = Bitmap.createBitmap(500, 800, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(4);
        //绘制矩形
        float rectStartX = 30;
        float rectStartY = 50;
        float rectStopX = 500;
        float rectStopY = 250;
        canvas.drawRect(rectStartY, rectStartY, rectStopX, rectStopY, paint);

        paint.setStrokeWidth(1);
        //随机绘制100条线
        float startX, startY, endX, endY;
        for (int i = 0; i < 100; i++) {
            double random1 = Math.random();
            if (random1 <= 0.5) {
                double random2 = Math.random();
                startX = rectStartX;
                startY = (float) (random2 * (rectStopY - rectStartY) + rectStartY);

                double random3 = Math.random();
                if (random2 <= 0.3) {
                    endX = (float) (random3 * (rectStopX - rectStartX) + rectStartX);
                    endY = rectStartY;
                } else if (random2 <= 0.7) {
                    endX = rectStopX;
                    endY = (float) (random3 * (rectStopY - rectStartY) + rectStartY);
                } else {
                    endX = (float) (random3 * (rectStopX - rectStartX) + rectStartX);
                    endY = rectStopY;
                }
            } else {
                double random2 = Math.random();
                startX = (float) (random2 * (rectStopX - rectStartX) + rectStartX);
                startY = rectStartY;

                double random3 = Math.random();
                if (random2 <= 0.3) {
                    endX = rectStopX;
                    endY = (float) (random3 * (rectStopY - rectStartY) + rectStartY);
                } else if (random2 <= 0.7) {
                    endX = (float) (random3 * (rectStopX - rectStartX) + rectStartX);
                    endY = rectStopY;
                } else {
                    endX = rectStartX;
                    endY = (float) (random3 * (rectStopY - rectStartY) + rectStartY);
                }
            }
            paint.setColor(Color.rgb((float) (Math.random() * 255), (float) (Math.random() * 255), (float) (Math.random() * 255)));
            canvas.drawLine(startX, startY, endX, endY, paint);
        }

        //随机绘制4个数字
        paint.setTextSize(40);
        paint.setStyle(Paint.Style.FILL);
        String[] text = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        for (int i = 0; i < 4; i++) {
            double random1 = Math.random();
            double random2 = Math.random();
            int index = (int) Math.round(random1 * 9);
            canvas.drawText(text[index],
                    (float) (random1 * (rectStopX - rectStartX - 40) + (rectStartX + 20)),
                    (float) (random2 * (rectStopY - rectStartY - 40) + (rectStartY + 20)), paint);
        }

        iv.setImageBitmap(bmp);
        return root;
    }
}
